package com.bonshotels.DataLayer.CtrlDadesHibernate;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;

import com.bonshotels.DomainLayer.CtrlDades.ICtrlClient;
import com.bonshotels.DomainLayer.DomainModel.Client;
import com.bonshotels.Excepcions.ClientNoExisteix;

/**
 * Aquesta classe es la implementacio concreta per a Hibernate de ICtrlClient, es un producte
 * concret dins del patro factoria abstracta.
 * @autor Marc Mauri Alloza
 */
public class CtrlClientHibernate extends CtrlGeneralHibernate implements ICtrlClient {

	/* (non-Javadoc)
	 * @see com.bonshotels.DomainLayer.CtrlDades.ICtrlClient#obte(java.lang.String)
	 */
	@Override
	public Client obte(String dni) throws ClientNoExisteix {
		Session session = getSession();
		Client c = (Client) session.get(Client.class, dni);
		if (c == null) throw new ClientNoExisteix();
		return c;
	}

	/* (non-Javadoc)
	 * @see com.bonshotels.DomainLayer.CtrlDades.ICtrlClient#existeix(java.lang.String)
	 */
	@Override
	public boolean existeix(String dni) {
		Query query = getSession().createQuery("select * from Client where dni = :dni");
		query.setParameter("dni", dni);
		return query.list().size() == 1;
	}

	/* (non-Javadoc)
	 * @see com.bonshotels.DomainLayer.CtrlDades.ICtrlClient#obteTots()
	 */
	@Override
	public Set<Client> obteTots() {
		Collection<Client> cs = getSession().createCriteria(Client.class).list();
		return new HashSet<Client>(cs);
	}

}
